home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Virtual Members: Difficult Question
- Date: Sat, 20 Jan 1996 19:07:34 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4dregt$i22@news.halcyon.com>
- References: <4dmha6$80c@marlin.ssnet.com>
- NNTP-Posting-Host: blv-pm3-ip2.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- helie@ssnet.com (Ray Helie) wrote:
-
- >I have a difficult problem ...
- >class C_BASE : public CObject
- > {
- > // ...
- > virtual void index () = 0;
- > };
-
-
- >I don't really use any of the functions out of CObject, but I have to
- >inherit it to pass C_BASE to other objects....
-
- >class C_ITEM : public C_BASE
- > {
- > // ...
- > void index () { ... };
- > };
-
-
- >...(and this part is a prelude to the tough
- >part):
-
- >void main ()
- > {
- > C_ITEM item;
- > func1 ((CObject*)&item);
- > }
-
- >void func1 (CObject* pItem)
- > {
- > C_BASE* base;
- > base = pItem;
- > pItem-> index ();
- > // (i) is the above call to index () allowed? or did i lose virtual
- > // information about the class C_BASE when I went to the CObject
- > // pointer?
- > }
-
-
- >That seems to work for me, but I thought I'd make sure it's something
- >that is truly legal that is supposed to work *all* the time.
-
- BTW, If func1 is calling pItem->index(), then func1 really *does* take
- C_BASE * or better, not CObject *. I'd reconsider the argument type
- to func1 first!
-
- Generally down-casts are unsafe. Is this VC4.0? You could try to
- dynamic_cast<C_BASE*>(pItem). Compile with RTTI turned on (/GR), then
- if pItem doesn't really point to a C_BASE (or derivative thereof),
- dynamic_cast will return NULL. This is part of a recent draft of C++
- to standardize run-time type info. Any set of classes with virtual
- functions will be operable this way.
-
- >On to the difficult part:
- >I'm reading and writing object states to disk. I've have need to do it
- >the following way, so what I need to know is why the following way
- >doesn't seem to work and how I can get around it:
-
-
- >main ()
- > {
- > C_ITEM item;
- > writeObject ((CObject*)&item,sizeof(C_ITEM));
- > readObject ((CObject*)&item,sizeof(C_ITEM));
- > }
-
-
- >void readObject (CObject* pObject,int pSize)
- > {
- > C_BASE* base = pObject;
- > base-> index (); // this call works fine
- > Read ((char*)&pObject,pSize);
- > // this reads in the character string from beginning of a file
-
- > base-> index (); // !!! this call now crashes the program !!!
-
- > }
-
-
- >void writeObject (CObject* pObject,int pSize)
- > {
- > Write ((char*)pObject,pSize);
- > // this will write a string of characters to the start of a file
- > // ...
- > }
-
-
- >So why does the line in readObject () crash...
- >Ray Helie [helie@ssnet.com]
-
- It would be preferrable, of course, to just override CObject's
- Serialize() method and persist data member by member, each derivation
- remembering to call it's immediate base class' Serialize(), etc.
-
- From the sounds of it, the blanket read of sizeof(C_ITEM)
- bytes for pObject corrupts the vtable of the C_BASE reference.
- Perhaps the debugger could shed some light. Anyway, dynamic_cast
- *might* solve the problem if you're dead-set against using the
- customary Serialize() approach.
-
- Hope this helps.
- --Norm
-
-